<?php
function getAlexaRank($domain) {
	$request = "http://data.alexa.com/data?cli=10&amp;dat=s&amp;url=" . $domain;
	$data = getPageData($request);
	preg_match('/<POPULARITY URL="(.*?)" TEXT="([\d]+)"\/>/si', $data, $p);
	$value = ($p[2]) ? $p[2] : "n/a";
	#$string = "<a href=\"http://www.alexa.com/data/details/traffic_details?url=" . $domain . "\">" . number_format($value) . "</a>";
	$string = $value;
	return $string;
}
/* ============================== common function ========================================*/
function getPageData($url) 
{
	if(function_exists('curl_init')) {
		$ch = curl_init($url); // initialize curl with given url
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // add useragent
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
		if((ini_get('open_basedir') == '') && (ini_get('safe_mode') == 'Off')) {
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
		}
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // max. seconds to execute
		curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
		return @curl_exec($ch);
	}
	else {
		return @file_get_contents($url);
	}
} ?>